package com.lhd.app.metrics;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.AggregateFunction;

/**
 * 商品指标聚合函数（最终修复版）
 */
public class GoodsMetricAggregateFunction implements AggregateFunction<JSONObject, GoodsMetricsAccumulator, GoodsMetricsAccumulator> {

    @Override
    public GoodsMetricsAccumulator createAccumulator() {
        return new GoodsMetricsAccumulator();
    }

    @Override
    public GoodsMetricsAccumulator add(JSONObject value, GoodsMetricsAccumulator accumulator) {
        try {
            JSONObject common = value.getJSONObject("common");
            JSONObject action = value.getJSONObject("action");
            JSONObject page = value.getJSONObject("page");

            String mid = common.getString("mid");
            String actionId = action != null ? action.getString("action_id") : null;
            String pageId = page.getString("page_id");
            String lastPageId = page.getString("last_page_id");

//            System.out.println("=== 处理数据 ===");
//            System.out.println("mid: " + mid + ", pageId: " + pageId +
//                    ", lastPageId: " + lastPageId + ", actionId: " + actionId);

            // 情况1：当前页面是商品详情页（统计PV/UV）
            if ("good_detail".equals(pageId)) {
                accumulator.pv++;
                accumulator.mids.add(mid);
//                System.out.println("✅ 商品详情页访问: PV+1, UV添加用户: " + mid);

                // 情况1.1：在商品详情页内的收藏行为
                if ("favor_add".equals(actionId)) {
                    accumulator.favorMids.add(mid);
//                    System.out.println("✅ 商品详情页内收藏: 收藏用户添加: " + mid);
                }

                // 情况1.2：在商品详情页内的加购行为（如果有的话）
                if ("cart_add_num".equals(actionId)) {
                    accumulator.cartMids.add(mid);
//                    System.out.println("✅ 商品详情页内加购: 加购用户添加: " + mid);
                }
            }

            // 情况2：从商品详情页跳转后的加购行为（在购物车页面）
            if ("good_detail".equals(lastPageId) && "cart_add_num".equals(actionId)) {
                accumulator.cartMids.add(mid);
//                System.out.println("✅ 从商品详情页跳转后加购: 加购用户添加: " + mid);
            }

            // 情况3：从商品详情页跳转后的收藏行为（在其他页面）
            if ("good_detail".equals(lastPageId) && "favor_add".equals(actionId)) {
                accumulator.favorMids.add(mid);
//                System.out.println("✅ 从商品详情页跳转后收藏: 收藏用户添加: " + mid);
            }

//            System.out.println("当前统计: PV=" + accumulator.pv + ", UV=" + accumulator.mids.size() +
//                    ", 加购=" + accumulator.cartMids.size() + ", 收藏=" + accumulator.favorMids.size());

        } catch (Exception e) {
            System.err.println("数据处理错误: " + value.toJSONString());
            e.printStackTrace();
        }

        return accumulator;
    }

    @Override
    public GoodsMetricsAccumulator getResult(GoodsMetricsAccumulator accumulator) {
//        System.out.println("=== 窗口最终结果 ===");
//        System.out.println("PV=" + accumulator.pv + ", UV=" + accumulator.mids.size() +
//                ", 加购UV=" + accumulator.cartMids.size() + ", 收藏UV=" + accumulator.favorMids.size());
        return accumulator;
    }

    @Override
    public GoodsMetricsAccumulator merge(GoodsMetricsAccumulator a, GoodsMetricsAccumulator b) {
        a.pv += b.pv;
        a.mids.addAll(b.mids);
        a.cartMids.addAll(b.cartMids);
        a.favorMids.addAll(b.favorMids);
        return a;
    }
}